<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">投票名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="投票名称不能为空"
                   placeholder="请输入投票名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">投票/评分</label>
        <div class="layui-input-block">
            <input type="checkbox" name="type" lay-skin="switch" lay-text="投票|评分">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">投票日期</label>
        <div class="layui-input-block">
            <input type="text" id="startTime" lay-verify="required" lay-reqtext="投票日期不能为空" name="startTime"
                   class="layui-input" placeholder="投票日期">
        </div>
    </div>

    <!--    <div class="layui-form-item">-->
    <!--        <label class="layui-form-label required">最大投票数</label>-->
    <!--        <div class="layui-input-block">-->
    <!--            <input type="number" value="1" name="voteLimit" lay-verify="required" lay-reqtext="最大投票数不能为空"-->
    <!--                   placeholder="请输入最大投票数" value="" class="layui-input">-->
    <!--        </div>-->
    <!--    </div>-->
    <!--    <div class="layui-form-item">-->
    <!--        <label class="layui-form-label">是否必须全部投票/评分</label>-->
    <!--        <div class="layui-input-block">-->
    <!--            <input type="checkbox" name="voteMaxLimit" lay-skin="switch" lay-text="是|否" checked>-->
    <!--        </div>-->
    <!--    </div>-->
    <div class="layui-form-item">
        <label class="layui-form-label">候选人</label>
        <div class="layui-input-block">
            <input type="file" id="file1"
                   accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">评委</label>
        <div class="layui-input-block">
            <input type="file" id="file2"
                   accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">打分选项</label>
        <div class="layui-input-block">
            <input type="file" id="file3"
                   accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注信息</label>
        <div class="layui-input-block">
            <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../js/common.js"></script>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let split = data.field.startTime.split(" - ");
            data.field.startTime = split[0];
            data.field.expiresTime = split[1];
            if (data.field.type === "on") {
                data.field.type = 1;
            } else {
                data.field.type = 2;
            }
            console.log(data.field)
            const formData = new FormData();
            for (const key in data.field) {
                formData.append(key, data.field[key]);
            }
            formData.append("files", $("#file1")[0].files[0]);
            formData.append("files", $("#file2")[0].files[0]);
            if ($("#file3")[0].files[0]) {
                formData.append("files", $("#file3")[0].files[0]);
            }
            $.ajax({
                url: `/item/createVote`,
                type: "post",
                data: formData,
                dataType: 'json',
                processData: false,
                contentType: false,
                success: function (res) {
                    checkLogin(res);
                    parent.layer.msg(res.msg, {icon: 6, time: 1000}, function () {
                        parent.layui.table.reload('currentTableId', {});
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                }
            })
            return false;
        });

        //日期范围选择
        laydate.render({
            elem: '#startTime',
            type: 'datetime',
            range: true //或 range: '~' 来自定义分割字符
        });


    });
</script>
</body>
</html>
